<template>
  <el-dialog :title="title" :visible.sync="isShow" @close="closeEvent">
    <el-form
      ref="form"
      prop="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="form"
    >
      <el-form-item label="企业编号" prop="eid">
        <el-input v-model.trim="form.eid"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="name">
        <el-input v-model.trim="form.name"></el-input>
      </el-form-item>
      <el-form-item label="企业简称" prop="short_name">
        <el-input v-model.trim="form.short_name"></el-input>
      </el-form-item>
      <el-form-item label="企业简介" prop="intro">
        <el-input v-model.trim="form.intro"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model.trim="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="center">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 导入新增接口
import { enterpriseAdd, enterpriseEdit } from '@/api/EnterPrise.js'

export default {
  name: 'enterPriseComponentIndex',
  data () {
    return {
      isShow: false,
      mode: 'add', //add是新增，edit是编辑
      form: {
        eid: '', //是	string	企业编号
        name: '', //	是	string	企业名称
        short_name: '', //	 是	string	简称
        intro: '', //是	string	企业简介
        remark: '', // 否	string	备注
        status: 1
      },
      rules: {
        eid: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 9, message: '请输入1-9位企业编号', trigger: 'change' }
        ],
        name: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 9, message: '请输入1-9位企业名称', trigger: 'change' }
        ],
        short_name: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 9, message: '请输入1-9位简称', trigger: 'change' }
        ],
        intro: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 9, message: '请输入1-9位备注', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        // console.log(result)
        if (result) {
          if (this.mode === 'add') {
            await enterpriseAdd(this.form)
            //  弹框
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await enterpriseEdit(this.form)
            this.$message.success('编辑成功')
          }

          //  关闭弹框
          this.isShow = false
          // 刷新父级组件
          this.$emit('getData')
        }
      })
    },
    // 关闭
    closeEvent () {
      this.$refs.form.resetFields() //移除校验结果
      this.form = {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      }
    }
  },
  computed: {
    // 标题处理
    title () {
      if (this.mode === 'add') {
        return '新增企业'
      } else {
        return '编辑企业'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.form {
  padding: 19;
}
::v-deep .el-dialog__header {
  text-align: center;
  background: linear-gradient(90deg, #01c5fa, #1394fa);
  .el-dialog__title {
    color: #fff;
  }
  .el-icon-close:before {
    color: #fff;
  }
}
.center {
  text-align: center;
}
</style>
